using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Package;
using MH.WsdlWorks.ProjectModel;
using System.Globalization;
using System.IO;
using OleConstants = Microsoft.VisualStudio.OLE.Interop.Constants;
using VsCommands = Microsoft.VisualStudio.VSConstants.VSStd97CmdID;
using VsCommands2K = Microsoft.VisualStudio.VSConstants.VSStd2KCmdID;
using Microsoft.VisualStudio.Shell.Interop;

namespace MH.WsdlWorks
{
    public class MessageFileNode : SimpleFileNode
    {
        readonly MessageFile messageFile;

        public MessageFile MessageFile
        {
            get { return messageFile; }
        }

        public MessageFileNode(SimpleProjectNode root, SimpleProjectElement e, MessageFile messageFile)
            : base(root, e)
        {
            this.messageFile = messageFile;
        }

        /// <summary>
        /// Do not allow the file to be renamed
        /// </summary>
        /// <param name="label"></param>
        /// <returns></returns>
        public override int SetEditLabel(string label)
        {
            throw new InvalidOperationException("The request file cannot be renamed.");
            //return VSConstants.E_FAIL;
        }

        /// <summary>
        /// cannot delete the request file, the user should delete the whole request.
        /// </summary>
        /// <param name="deleteOperation"></param>
        /// <returns></returns>
        protected override bool CanDeleteItem(Microsoft.VisualStudio.Shell.Interop.__VSDELETEITEMOPERATION deleteOperation)
        {
            return false;
        }

        protected override SimpleNodeProperties CreatePropertiesObject()
        {
            return new MessageFileNodeProperties(this);
        }

        protected override int QueryStatusOnNode(Guid guidCmdGroup, uint cmd, IntPtr pCmdText, ref QueryStatusResult result)
        {
            if (guidCmdGroup == GuidList.guidTestMenuPackageCmdSet)
            {
                switch (cmd)
                {
                    case PackageCommandIdList.cmdidOpen:
                    case PackageCommandIdList.cmdidOpenWith:
                        result |= QueryStatusResult.SUPPORTED | QueryStatusResult.ENABLED;
                        return VSConstants.S_OK;
                }
            }
            return base.QueryStatusOnNode(guidCmdGroup, cmd, pCmdText, ref result);
        }

        protected override int ExecCommandOnNode(Guid guidCmdGroup, uint cmd, uint nCmdexecopt, IntPtr pvaIn, IntPtr pvaOut)
        {
            if (guidCmdGroup == GuidList.guidTestMenuPackageCmdSet)
            {
                IVsWindowFrame windowFrame = null;
                switch (cmd)
                {
                    case PackageCommandIdList.cmdidOpen:
                        return OpenFile();
                    case PackageCommandIdList.cmdidOpenWith:
                        return ((SimpleFileDocumentManager)this.GetDocumentManager()).Open(false, true, VSConstants.LOGVIEWID_UserChooseView, out windowFrame, WindowFrameShowAction.Show);
                }
            }
            return base.ExecCommandOnNode(guidCmdGroup, cmd, nCmdexecopt, pvaIn, pvaOut);
        }

        public int OpenFile()
        {
            return ((SimpleFileDocumentManager)this.GetDocumentManager()).Open(false, false, WindowFrameShowAction.Show);
        }

        public override int MenuCommandId
        {
            get
            {
                return MenuIdList.menuIdMessageFileNode;
            }
        }
    }
}
